package com.store.app.activity;

import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.store.app.BaseActivity;
import com.store.app.MainActivity;
import com.store.app.R;
import com.store.app.adapter.InvitationAdapter;
import com.store.app.bean.EventBusBean;
import com.store.app.bean.GuideShoppingAgreeBean;
import com.store.app.http.Stores;
import com.store.app.net.NetRequest;
import com.store.app.net.ifc.IOnHttpListener;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * Created by Administrator on 2017/2/10.
 * 邀请会员页面
 */
public class InvitationMemberActivity extends BaseActivity implements IOnHttpListener {
    private ImageView public_ll_return;
    private TextView tvTitle;
    private EditText et_search;
    private ListView lv;
    private NetRequest netRequest;
    private InvitationAdapter guideShoppingAgreeAdapter;
    private List<GuideShoppingAgreeBean> list;
    private TextView public_tv_right;
    private Dialog dialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_invitation_member);
        initView();

    }

    private void initView() {
        public_ll_return = (ImageView) findViewById(R.id.public_ll_return);
        tvTitle = (TextView) findViewById(R.id.tvTitle);
        public_ll_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        tvTitle.setText("邀请会员");
        et_search = (EditText) findViewById(R.id.et_search);
        lv = (ListView) findViewById(R.id.lv);
        netRequest = new NetRequest(this);
        list = new ArrayList<>();
        guideShoppingAgreeAdapter = new InvitationAdapter(list, this, new InvitationAdapter.OnClickOrder() {
            @Override
            public void invitaClick(final GuideShoppingAgreeBean bean) {

                AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(InvitationMemberActivity.this);
                dialogBuilder.create();
                LayoutInflater inflater = LayoutInflater.from(InvitationMemberActivity.this);
                View view = inflater.inflate(R.layout.if_invitation_dialog, null);
                TextView submit = (TextView) view.findViewById(R.id.submit);
                TextView cancel = (TextView) view.findViewById(R.id.cancel);
                submit.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                        netRequest.requestSaleAssistantInvite(2, bean.getConsumer_id(), bean.getMobile(), MainActivity.member_id, Stores.stores_name);
                    }
                });
                cancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        dialog.dismiss();
                    }
                });
                dialogBuilder.setView(view);
                dialog = dialogBuilder.show();

            }
        });
        lv.setAdapter(guideShoppingAgreeAdapter);
        et_search.setOnEditorActionListener(new TextView.OnEditorActionListener() {

            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    //requestSearch();
                    if (TextUtils.isEmpty(et_search.getText().toString().trim())) {
                        showToast("请输入会员手机号");
                    } else {
                        list.clear();
                        guideShoppingAgreeAdapter.notifyDataSetChanged();
                        showProgressDialog("搜索会员", "正在搜索...");
                        netRequest.requestVailsMemberExist(1, et_search.getText().toString());
                    }
                }
                return false;
            }
        });


    }

    @Override
    public void onAfinalFail() {
        dismissProgressDialog();
        showToast("请检查本地网络");
    }

    @Override
    public void onExecuteSuccess(int requestId, String result, String msg) {
        dismissProgressDialog();
        if (requestId == 1) {
            Log.v("zyl", "result:" + result);
            try {
                JSONObject jsonObject = new JSONObject(result);
                String address = jsonObject.getString("address");
                String consumer_id = jsonObject.getString("consumer_id");
                String mobile = jsonObject.getString("mobile");
                String nick_name = jsonObject.getString("nick_name");
                GuideShoppingAgreeBean guideShoppingAgreeBean = new GuideShoppingAgreeBean();
                guideShoppingAgreeBean.setAddress(address);
                guideShoppingAgreeBean.setConsumer_id(consumer_id);
                guideShoppingAgreeBean.setMobile(mobile);
                guideShoppingAgreeBean.setNick_name(nick_name);
                list.clear();
                list.add(guideShoppingAgreeBean);
                guideShoppingAgreeAdapter.notifyDataSetChanged();
            } catch (Exception e) {

            }
        } else if (requestId == 2) {
            et_search.setText("");
            EventBus.getDefault().post(new EventBusBean(226));
            showToast("已成功邀请会员");
        }
    }

    @Override
    public void onExecuteFail(int requestId, String msg) {
        dismissProgressDialog();
        showToast(msg);
    }
}
